package com.example.demo.controller;
import com.example.demo.config.AjaxResult;
import com.example.demo.model.UserInfo;
import com.example.demo.service.CommentService;
import com.example.demo.util.ConstVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping("/add")
    public Object addComment(HttpServletRequest request, int pid,String traceNo,String content,int rating){
        HttpSession session = request.getSession(false);
        UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
        int uid = userInfo.getId();
        return AjaxResult.success("评论发表成功！", commentService.addComment(uid, pid,traceNo,content,rating));
    }

    @RequestMapping("/get-all")
    public Object getComment(int pid){
        return AjaxResult.success("获取评论成功！", commentService.getCommentByPid(pid));
    }

    @RequestMapping("/get-by-rating")
    public Object getCommentByRating(String rating,int pid){
        switch (rating) {
            case "all" -> {
                int minRating = 0;
                int maxRating = 5;
                return AjaxResult.success("获取评论成功！", commentService.getCommentByRating(minRating, maxRating, pid));
            }
            case "positive" -> {
                int minRating = 4;
                int maxRating = 5;
                return AjaxResult.success("获取评论成功！", commentService.getCommentByRating(minRating, maxRating, pid));
            }
            case "neutral" -> {
                int minRating = 3;
                int maxRating = 3;
                return AjaxResult.success("获取评论成功！", commentService.getCommentByRating(minRating, maxRating, pid));
            }
            case "negative" -> {
                int minRating = 0;
                int maxRating = 2;
                return AjaxResult.success("获取评论成功！", commentService.getCommentByRating(minRating, maxRating, pid));
            }
            default -> {
                return AjaxResult.fail(-1, "获取评论失败！");
            }
        }

    }
}
